import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/29 9:49
 * @description DFS 优化 time: 2ms
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    public boolean can = false;
    boolean[] visited ;
    public boolean canReach(int[] arr, int start) {
        int n = arr.length;
        visited = new boolean[n];
        dfs(start, arr);
        return can;
    }
    public void dfs(int index, int[] arr) {
        if (arr[index] == 0) {
            can = true;
        }
        if (can) {
            return;
        }
        if (visited[index]) {
            return;
        }
        visited[index] = true;
        if (index + arr[index] >= 0 && index + arr[index] < arr.length) {
            dfs(index + arr[index], arr);
        }
        if (index - arr[index] >= 0 && index - arr[index] < arr.length) {
            dfs(index - arr[index], arr);
        }
    }
}
